package com.aishua.fire.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.aishua.fire.entity.checkManager.CheckRecordRespInfo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ExcelUtil {
	
	@Value("${excel.template.dir}")
	private String excelRootDir;
	
	@Value("${project.imgupload.rootPath}")  
    private String serverRootPath;
	
	@Value("${server.path}")
	private String serverPath;
	
	@Value("${project.imgreq.rootPath}")
	private String mImagesReqPath;
	
	@Value("${server.port}")
	private String serverPort;
	
	public String checkRecordExcel(List<CheckRecordRespInfo> list, String excelTemppath, String excelCheck) {
		FileOutputStream fos = null;
		try {
			FileInputStream fis  =new FileInputStream(excelRootDir+"checkRecord_template.xls");
			POIFSFileSystem ps = new POIFSFileSystem(fis);
			
			HSSFWorkbook wb = new HSSFWorkbook(ps);
			HSSFSheet currentMonthSheet = wb.getSheetAt(0);
			for(int i=0;i<list.size();i++) {
				CheckRecordRespInfo record =list.get(i);
				HSSFRow row = currentMonthSheet.createRow(i+1);
				row.createCell(0).setCellValue(record.getPointName());
				row.createCell(1).setCellValue(record.getPointNum());
				row.createCell(2).setCellValue(record.getUserName());
				row.createCell(3).setCellValue(record.getCheckTime());
				row.createCell(4).setCellValue(record.getCheckTime());
				row.createCell(5).setCellValue(record.getSituation());
			}
			
			String rootPath = serverRootPath+excelTemppath+"/";
			File targetFile = new File(rootPath);  
			if(!targetFile.exists()){    
				targetFile.mkdirs();    
			}
			
			fos = new FileOutputStream(rootPath+excelCheck);
			
			wb.write(fos);
			wb.close();
			//拼接返回请求路径
			String reqPath = "http://"+serverPath+":"+serverPort+mImagesReqPath+excelTemppath+"/"+excelCheck;
			return reqPath;
		}catch (FileNotFoundException e) {
			log.error("{}",e);
		} catch (IOException e) {
			log.error("{}",e);
		}finally {
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					log.error("{}",e);
				}
			}
		}
		return "";
	}


}
